#!/usr/bin/python

import os
import numpy
from collections import Counter
from pylab import *
import tables,scipy
from scipy import fftpack
from scipy.cluster import vq
from scipy.ndimage import morphology,filters,interpolation
from ocrolib import docproc,improc
import pyflann
import random
import tables
from chario import confusable,load_random,load_balanced
from multiprocessing import Pool
import fcntl
from pdb import pm
from llpy import chario
from llpy.sutils import *

import argparse
parser = argparse.ArgumentParser( description = "Show nearest neighbor information.")
parser.add_argument('protos',default='protos.h5',help="data file")
parser.add_argument('data',default='patches.h5',help="data file")
parser.add_argument('-t','--table',default="/patches")
parser.add_argument('-s','--start',type=int,default=0)
parser.add_argument('-n','--nimages',default=10,type=int,help="number of patches to display")
args = parser.parse_args()

with tables.openFile(args.protos) as pdb, tables.openFile(args.data) as db:
    nprotos = len(pdb.root.patches)
    for i in range(args.start,nprotos):
        out = [pdb.root.patches[i]]
        out += [db.root.patches[pdb.root.neighbors[i,j]] for j in range(min(100,len(pdb.root.neighbors[i])))]
        out = array(out)
        print out.shape
        clf()
        gray()
        showgrid(out,r=10)
        ginput(1,36000)
